home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17344 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  67 lines

  1. Newsgroups: comp.lang.c,comp.lang.c++
  2. Path: uu4news.netcom.com!friend!news
  3. From: rich@kastle.com (Richard Krehbiel)
  4. Subject: Re: sizeof() question >>> :)
  5. Message-ID: <1996Apr15.113552.27625@friend.kastle.com>
  6. Sender: news@friend.kastle.com (News)
  7. Reply-To: rich@kastle.com
  8. Organization: Kastle Development Associates
  9. X-Newsreader: Forte Free Agent 1.0.82
  10. References: <1996Apr12.061927@topaz>
  11. Date: Mon, 15 Apr 1996 11:37:40 GMT
  12.  
  13. naderr@topaz.cqu.edu.au wrote:
  14.  
  15. >Hi,
  16.  
  17. >How can I get, with a pointer,  the sizeof  of an  array that is
  18. >pointed by the pointer ?  (No it's not a tounge twister :)
  19.  
  20. This information is simply not available if all you have is a pointer.
  21. You have to make size information available to yourself some other
  22. way.  I've added a few lines to your original program to suggest one
  23. way this can be done.  Specifically, I added another array "sizes"
  24. that parallels the pointer array "record", in which the sizes of the
  25. strings are stored.
  26.  
  27. >Ok, let see if with an example I  could clarify what I'm trying to say,
  28.  
  29. >  char First_name[20];
  30. >  char Last_name[20];
  31. >  char Address[60];
  32. >  char Phone[20];
  33. >  char **ct, *cp;
  34.    size_t *szp;
  35.  
  36. >  char *record[4];
  37.    size_t sizes[4];
  38.  
  39. >  char ch;
  40. >  
  41. >  record[0] = First_name;
  42.    sizes[0] = sizeof(First_name);
  43. >  record[1] = Last_name;
  44.    sizes[1] = sizeof(Last_name);
  45. >  record[2] = Address;
  46.    sizes[2] = sizeof(Address);
  47. >  record[3] = Phone;
  48.    sizes[3] = sizeof(Phone);
  49. >  ct = record;
  50. >  cp = *ct;
  51.    szp = sizes;
  52.  
  53. >  while (!done) {
  54. >    ch = getchar();
  55. >    if (cp - *ct < sizeof(XXXXXXX)-1) {
  56.      if (cp - *ct < *szp - 1)
  57. >      *cp++ = ch;
  58. >    } else {
  59. >      beep();
  60. >    }  
  61. >  }
  62.  
  63. --
  64. Richard Krehbiel, Kastle Systems, Arlington VA USA
  65. rich@kastle.com (work) or richk@mnsinc.com (personal)
  66.  
  67.